Module# 07: Queue Lecture#25: Programming
for Queue
/* The following set
of definitions followed by the main class illustrate the array implementation
of Queue. */
// Example 25.1:
Defining the class queue using array
// Example 25.2:
Defining the method enqueue()
// Example 25.3:
Defining the method dequeue()
// Example 25.4:
Defining the method isEmpty()
// Example 25.5:
Defining the method printQueue()
// Example 25.6:
Basic stack operations using the class QueueA
// The program
includes the above definitions followed by a demo class
// Example 25.1:
Defining the class queue using array
class QueueA<T>{
T[] data;
int front, rear;
int length;
QueueA(T[] a){
data = a;
front = 0;
rear = -1;
length = a.length;
}
// Example 25.2:
Defining the method enqueue()
void enque(T a){
if(rear>=length-1){
System.out.println("Queue
Overflow");
}
elss {
rear++;
data[rear] = a;
}
}
Example 25.3: Defining the method dequeue()
T deque(){
T x = null;
if(isEmpty()){
System.out.println("Queue
Underflow");
return null;
}
else {
x = data[front];
front++;
return x;
}
}
// Example 25.4:
Defining the method isEmpty()
boolean isEmpty(){
if(front>rear){
return true;
}
else{
return false;
}
}
// Example 25.5: Defining the method printQueue()
void printQueue() {
if(!isEmpty()) {
for(int i
= front;i<=rear;i++) {
System.out.print(data[i] + " ");
}
}
System.out.println();
}
} // End of the
definition of class QueueA
// Example 25.6:
Basic queue operation using the class QueueA
/* This is the main
program, illustration the usage of the class defined. You should include the
package, where this program is stored. */
class QueueAImplementationDemo
{
public static void main(String[] args){
Integer
arr[] = new Integer[2];
QueueA<Integer> q = new QueueA<Integer>(arr);
q.enque(1);
q.printQueue();
q.enque(2);
q.printQueue();
q.enque(3);
q.printQueue();
q.deque();
q.printQueue();
q.deque();
q.printQueue();
q.deque();
q.printQueue();
}
} // End of the demo class
/* The following set
of definitions followed by the main class illustrate the linked list
implementation of queue. */
// Example 25.7:
Defining the class queue using linked list
// Example 25.8:
Defining the method enqueue()
// Example 25.9:
Defining the method dequeue()
// Example 25.10:
Defining the method isEmpty()
// Example 25.11:
Defining the method printQueue()
// Example 25.12:
Basic Stack operation using the class QueueL
// The program
includes the above definitions followed by a demo class
// Example 25.7:
Defining the class queue using linked list
// This program shows
how a queue can be defined using a linked list
import jLLPacakge;
/* This program uses
linked list related implementation (e.g., the definitions available in
Lecture#18-19) ; so, include the directory (e.g., jLLPackage, where all those programs are defined. */
// This program shows
how a queue can be defined using a linked list
class QueueL<T> {
JLinkedList<T> front, rear;
QueueL(){
front = new JLinkedList<T>();
rear = front;
}
// Example 25.8:
Defining the method enqueue()
/* This definition
assumes the linked list implementation as discussed in Lecture# 18; Assume that
the programs are maintained in the directory: jLLPackage
folder. */
void enque(T a) {
this.rear.insertEnd(a); // Method is in jLLPackage
}
// Example 25.9:
Defining the method dequeue()
/* This definition
assumes the linked list implementation as discussed in Lecture# 19; Assume that
the programs are maintained in the directory: jLLPackage
folder. */
T deque(){
T data = null;
if(!isEmpty()){
this.front.deleteFront(); // Method is in jLLPackage
}
else {
System.out.print("Queue
Underflow");
}
return null;
}
// Example 25.10:
Defining the method isEmpty()
boolean isEmpty() {
if(front.isEmpty()){
return true;
}
else{
return false;
}
}
// Example 25.11:
Defining the method printQueue()
void printQueue(){
if(this.front.isEmpty()){
System.out.println("Queue is
Empty");
}
else{
this.front.printList();
}
}
} // End of the
definition of the class QueueL
// The main program
illustrating the linked list implementation of queue
// Example 25.12:
Basic queue operation using the class QueueL
/* This is the main
program, illustration the usage of the class defined. You should include the
package, where this program is stored. */
class QueueLImplementationDemo
{
public static void main(String[] args) {
QueueL<Integer> q = new QueueL<Integer>();
q.enque(1);
q.printQueue();
q.enque(2);
q.printQueue();
q.enque(3);
q.printQueue();
q.deque();
q.printQueue();
q.deque();
q.printQueue();
q.deque();
q.printQueue();
}
} // End of the demo class
// Example 25.12:
Process scheduling application of queue
/* The following is
the class definition to define a process. */
class Process {
int index;
int time;
Process(int i,int t){
time = t;
index = i;
}
void print()
{
System.out.println("Job:"+ this.index + " time:" + this.time);
}
} // End of the process class
/* The following
program is the main class which schedules processes according to their
priorities. */
class QueueApplicationDemo
{
public static void main(String[] args) {
Process
arr1 = new Job(1,5);
Process arr2 = new Job(2,15);
Process
arr3 = new Job(3,10);
Process
arr4 = new Job(4,35);
Process
done;
int totaltime
= 0;
QueueL<Job> fcfs = new QueueL<Job>();
fcfs.enque(arr1);
fcfs.enque(arr2);
fcfs.enque(arr3);
fcfs.enque(arr4);
}
} // End of the driver class